package com.sinven.controller.manage;

import com.sinven.common.ActionResult;
import com.sinven.pojo.Article;
import com.sinven.service.ArticleService;
import com.sinven.util.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/manage")
public class ArticleController {
    @Autowired
    private ArticleService articleService;

    @GetMapping("articles")
    public ActionResult<List<Article>> getArticles(@RequestParam("code") String code,
                                                   @RequestParam(value = "page",defaultValue = "1") Integer page,
                                                   @RequestParam(value = "limit",defaultValue = "10") Integer pageSize){
        return articleService.queryArticles(code, page, pageSize);
    }

    @GetMapping("articles/{id}")
    public ActionResult<Article> getArticle(@PathVariable("id")long id){
        Article article = articleService.queryArticle(id);
        return ResultUtil.success(article);
    }
    @PostMapping("articles")
    public ActionResult<Void> addArticle(Article article){
        boolean result = articleService.addArticle(article);
        if (result){
            return ResultUtil.success(null);
        }
        return ResultUtil.error(500,"添加失败");
    }
    @PutMapping("articles")
    public ActionResult<Void> updateArticle(@RequestBody Article article){
        boolean result = articleService.updateArticle(article);
        if (result){
            return ResultUtil.success(null);
        }
        return ResultUtil.error(500,"修改失败");
    }
    /**
     * 删除文章
     *
     * @param articleId 要删除的文章ID
     * @return
     */
    @DeleteMapping("/articles/{id}")
    public boolean deleteArticle(@PathVariable("id") long articleId) {
        return articleService.deleteArticle(articleId);
    }
}
